package com.zyit.service;

import com.google.gson.Gson;
import com.zyit.DataObject.T_classify;
import com.zyit.service.Impl.ClassifyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/contendisplay")
public class ContendisplayService extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ClassifyService classifyService = new ClassifyServiceImpl();
        Integer type = Integer.valueOf(req.getParameter("id"));

        String parent_id = req.getParameter("parent_id");
        try {
            if (parent_id == null) {
                List<T_classify> typeClassify = classifyService.findTypeClassify(type);
                typeClassify.forEach(e -> System.out.println(e));
                System.out.println("77777");

                // 使用 JSON 序列化库将列表转换为 JSON 字符串
                String json = new Gson().toJson(typeClassify);

                // 设置响应的内容类型为 JSON
                resp.setContentType("application/json");

                // 设置响应的字符编码为 UTF-8
                resp.setCharacterEncoding("UTF-8");

                // 获取响应的输出流并将 JSON 写入响应
                resp.getWriter().write(json);
            } else {
                Integer integer = Integer.valueOf(parent_id);
                List<T_classify> nameClassify = classifyService.findNameClassify(type, integer);
                nameClassify.forEach(e -> System.out.println(e));
                System.out.println("8888");
                // 使用 JSON 序列化库将列表转换为 JSON 字符串
                String json = new Gson().toJson(nameClassify);

                // 设置响应的内容类型为 JSON
                resp.setContentType("application/json");

                // 设置响应的字符编码为 UTF-8
                resp.setCharacterEncoding("UTF-8");

                // 获取响应的输出流并将 JSON 写入响应
                resp.getWriter().write(json);


            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
